package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.mapper.FriendListMapper;
import com.example.blog.mapper.FriendRequestMapper;
import com.example.blog.pojo.FriendList;
import com.example.blog.pojo.FriendRequest;
import com.example.blog.pojo.Result;
import com.example.blog.service.FriendRequestService;
import com.example.blog.util.TokenUtil.decryptToken;
import com.example.blog.util.TokenUtil.spiltUtils;
import com.example.blog.util.UserUtils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class FriendRequestServiceImpl extends ServiceImpl<FriendRequestMapper, FriendRequest> implements FriendRequestService {
    @Resource
    private FriendRequestMapper friendRequestMapper;
    @Resource
    private FriendListMapper friendListMapper;
    @Resource
    private UserUtils utils;

    @Override
    public Result<T> fromFriendsRequest(Integer friendId, String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        Integer id = utils.getUserId(userName);
        FriendRequest friendRequest = new FriendRequest();
        friendRequest.setUserId(id);
        friendRequest.setFriendId(friendId);
        friendRequest.setCreateTime(new Date());
        int insert = friendRequestMapper.insert(friendRequest);
        if (insert > 0) {
            return Result.success("好友请求发送成功！");
        }
        return Result.failed("好友请求发送失败！");
    }

    @Override
    public Result<List<FriendRequest>> showFriendsApply(String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        Integer id = utils.getUserId(userName);
        List<FriendRequest> friendsApply = friendRequestMapper.showFriendsApply(id);
        return Result.success(friendsApply);
    }

    @Override
    public Result<T> addFriends(int friendId, String token, String isApply) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        Integer id = utils.getUserId(userName);
        UpdateWrapper<FriendRequest> updateWrapper = new UpdateWrapper<>();
        if (Objects.equals(isApply, "ture")) {
            updateWrapper.eq("user_id",friendId).eq("friend_id",id).set("status",0);
            int update0 = friendRequestMapper.update(null, updateWrapper);
            if (update0 > 0) {
                FriendList friendList = new FriendList();
                friendList.setFriendId(friendId);
                friendList.setUserId(id);
                int isFriends = friendListMapper.insert(friendList);
                if (isFriends > 0) {
                    return Result.success("你已同意好友申请！");
                }
                return Result.failed("出现了意料之外的问题,请重新操作！");
            }
            return Result.failed("出现了意料之外的问题,请重新操作！");
        } else if (Objects.equals(isApply, "false")) {
            updateWrapper.eq("user_id",friendId).eq("friend_id",id).set("status",1);
            int update1 = friendRequestMapper.update(null, updateWrapper);
            if (update1 > 0) {
                return Result.success("你已拒绝好友申请！");
            }
            return Result.failed("出现了意料之外的问题,请重新操作！");
        }
        return Result.failed("出现了意料之外的问题,请重新操作！");
    }
}
